$(() => {
   $('#td').click(function () {
      localStorage.setItem('Token', '')
      location.href = '/'
   })
   const nickname = $('.nickname')
   const uid = $('.uid')
   function upinfo() {
      $.get('/api/me', res => {
         if (res.code == 200) {
            nickname.text(res.nickname)
            uid.text(res.account)
         }
      })
   }
   upinfo()
   const fixed = $('.fixed')
   const upimg = $('.fixed .upimg')
   const editnickname = $('.editnickname')
   const modify_nickname_input = $('.modify-nickname-input')
   const c = $('.fixed .c')
   $('#upimg input').on('change', function () {
      let [file] = this.files
      let formData = new FormData();
      formData.append('files[0]', file, file.name);
      $.ajax({
         url: '/api/modify/profile',
         type: 'post',
         data: formData,
         contentType: false,
         processData: false,
         success(res) {
            if (res.code == 200) {
               $.get('/api/me', res => {
                  if (res.code == 200)
                     $('.profile_image').attr('src', '/profile/' + res.profile || '').parent().show().siblings('.login').hide()
               })
               alterBlock('上传成功')
               c.click()
            } else {
               c.click()
               alterBlock('上传失败，错误码：' + res.code)
            }
         }
      })
   })
   $('.list-box .list-button').on('click', function () {
      const t = $(this)
      const box = t.parents('.list-box:eq(0)')
      if (!box) return
      const i = t.index()
      ars(t, 'active')
      ars(box.find('.list-option').eq(i), 'dis')
   })
   document.addEventListener('click', function (event) {
      $('.fixed .error').removeClass('error')
   }, {
      capture: true
   })
   $('.fixed .c').on('click', function () {
      fixed.hide()
      upimg.hide()
      editnickname.hide()
   })
   $('.general-info-avatar').on('click', function () {
      fixed.show()
      upimg.show()
   })
   $('#edit-nickname').on('click', function () {
      fixed.show()
      editnickname.show()
   })
   $('#modify-nickname').on('click', function () {
      const v = modify_nickname_input.val().trim()
      if (v) {
         $.post('/api/modify/nickname', {
            nickname: v
         }, res => {
            if (res.code == 200) {
               upinfo()
               alterBlock('修改成功')
               c.click()
            }
         })
      } else {
         modify_nickname_input.addClass('error')
      }
   })
})